aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/sujet/[slug].tsx
blob: f6571e1ccb78d31066d497abf75f2ad4a7cde473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { getLayout } from '@components/Layouts/Layout';
import PostPreview from '@components/PostPreview/PostPreview';
import { t } from '@lingui/macro';
import { NextPageWithLayout } from '@ts/types/app';
import { SubjectProps } from '@ts/types/taxonomies';
import { loadTranslation } from '@utils/helpers/i18n';
import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
import Image from 'next/image';
import { ParsedUrlQuery } from 'querystring';
import styles from '@styles/pages/Subject.module.scss';
import {
  getAllSubjectsSlug,
  getSubjectBySlug,
} from '@services/graphql/queries';

const Subject: NextPageWithLayout<SubjectProps> = ({ subject }) => {
  const getPostsList = () => {
    return subject.posts.reverse().map((post) => (
      <li key={post.id} className={styles.item}>
        <PostPreview post={post} titleLevel={3} />
      </li>
    ));
  };

  return (
    <article>
      <header>
        <h1 className={styles.title}>
          {subject.featuredImage && (
            <span className={styles.cover}>
              <Image
                src={subject.featuredImage.sourceUrl}
                alt={subject.featuredImage.altText}
                layout="fill"
              />
            </span>
          )}
          {subject.title}
        </h1>
        {subject.officialWebsite && (
          <dl>
            <dt>{t`Official website:`}</dt>
            <dd>{subject.officialWebsite}</dd>
          </dl>
        )}
        <div dangerouslySetInnerHTML={{ __html: subject.intro }}></div>
      </header>
      <div dangerouslySetInnerHTML={{ __html: subject.content }}></div>
      {subject.posts.length > 0 && (
        <div>
          <h2>{t`All posts in ${subject.title}`}</h2>
          <ol className={styles.list}>{getPostsList()}</ol>
        </div>
      )}
    </article>
  );
};

Subject.getLayout = getLayout;

interface PostParams extends ParsedUrlQuery {
  slug: string;
}

export const getStaticProps: GetStaticProps = async (
  context: GetStaticPropsContext
) => {
  const translation = await loadTranslation(
    context.locale!,
    process.env.NODE_ENV === 'production'
  );
  const { slug } = context.params as PostParams;
  const subject = await getSubjectBySlug(slug);
  const breadcrumbTitle = subject.title;

  return {
    props: {
      breadcrumbTitle,
      subject,
      translation,
    },
  };
};

export const getStaticPaths: GetStaticPaths = async () => {
  const allSlugs = await getAllSubjectsSlug();

  return {
    paths: allSlugs.map((post) => `/sujet/${post.slug}`),
    fallback: true,
  };
};

export default Subject;